import requests

def main():
    # 请求头
    headers = {
        # 浏览器类型
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/99.0.4844.51 Safari/537.36 '
    }
    # 链接
    url = 'https://www.zhihu.com/api/v4/people/um-ah/collections?include=data%5B*%5D.updated_time%2Canswer_count' \
          '%2Cfollower_count%2Ccreator%2Cdescription%2Cis_following%2Ccomment_count%2Ccreated_time%3Bdata%5B*%5D.creator' \
          '.vip_info&offset=0&limit=20 '
    #获得请求
    response =getRes(headers,url)
    #得到收藏夹数据
    fav_data = response['data']
    #打印数据
    dataFormat(fav_data)

def getRes(headers,url):
    #返回请求的json
    return requests.get(url=url, headers=headers).json();

def dataFormat(data):
    headers = {
        # 浏览器类型
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/99.0.4844.51 Safari/537.36 '
    }
    # 循环数据内容
    for i in range(0, len(data)):
        # 收藏夹id
        fav_id = str(data[i]['id'])
        # 收藏夹标题
        fav_title = str(data[i]['title'])
        print('--------------------------' + fav_id + ':' + fav_title + '--------------------------')
        # 请求每个收藏夹
        url = 'https://www.zhihu.com/api/v4/collections/' + fav_id + '/items?offset=0&limit=20'
        response = requests.get(url=url,headers=headers).json()
        # 获取收藏夹内的数据
        ques_data = response['data']
        # 循环打印收藏夹的内容
        for j in range(0, len(ques_data)):
            ques_title = ques_data[j]['content']['question']['title']
            ques_url = ques_data[j]['content']['question']['url']
            print(ques_title + ":" + ques_url)

if __name__ == '__main__':
    main()